<template>
    <div class="container">

        <div class="body_wrap">

            <el-card shadow="hover" ref="content_card">

                <!-- 基本搜索+功能按钮 -->
                <el-form ref="searchForm1" :inline="true" :model="searchForm" size="medium" label-width="">
                    <el-card class="table_tool_bar_wrap" shadow="never">
                        <div class="base_wrap">
                            <div class="left">
                                <el-form-item label="代理商" prop="agentId">
                                    <el-select :loading="loading" placeholder="请选择" v-model="searchForm.agentId" filterable clearable>
                                        <el-option
                                            v-for="item in agentListSearch"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="关键字搜索" prop="keyword">
                                    <el-input placeholder="" v-model="searchForm.keyword" clearable/>
                                </el-form-item>
                                <el-button @click="handleSearch" type="primary" size="medium">搜索</el-button>
                                <el-button @click="handleReset" type="info" plain size="medium">重置</el-button>
                            </div>

                            <div class="right">
                                <el-button @click="handleAddOrUpdateVisible()" type="primary" size="medium" icon="el-icon-plus">新增</el-button>
                                <el-button @click="handleDelete()" type="danger" size="medium" icon="el-icon-delete" :disabled="selectionRows.length == 0">删除</el-button>
                                <el-button @click="handleBatchStockDialog()" type="primary" size="medium" icon="el-icon-delete" :disabled="selectionRows.length == 0">批量设置库存</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-form>


                <!-- 表格 -->
                <el-table
                    class="mt10"
                    ref="table"
                    size="medium"
                    height="500"
                    empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit border highlight-current-row
                    :stripe="stripe"
                    v-loading="loading"
                    :data="dataList"
                    @selection-change="handleSelection">

                    <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
                    <el-table-column label="序号" type="index" prop="index" v-if="isHideColumn('index')" width="60" align="center" />
                    <el-table-column label="代理商" prop="agentName" v-if="isHideColumn('agentName')" align="left" width="200" show-overflow-tooltip/>
                    <el-table-column label="SKU_ID" prop="skuId" v-if="isHideColumn('skuId')" align="left" width="200" show-overflow-tooltip/>
                    <el-table-column label="商品名称" prop="skuName" v-if="isHideColumn('skuName')" align="left" width="200" show-overflow-tooltip/>
                    <el-table-column label="商品规格" prop="skuAttr" v-if="isHideColumn('skuAttr')" align="left" show-overflow-tooltip/>
                    <el-table-column label="总库存数" prop="stock" v-if="isHideColumn('stock')" align="left" show-overflow-tooltip/>
                    <el-table-column label="出库" prop="stockOut" show-overflow-tooltip/>
                    <el-table-column label="剩余" prop="stock">
                        <template slot-scope="scope">
                            {{scope.row.stock > 0 ? scope.row.stock - scope.row.stockOut : 0}}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="锁定库存" prop="stockLocked" v-if="isHideColumn('stockLocked')" align="left" show-overflow-tooltip/> -->
                    <el-table-column label="创建时间" prop="ctime" v-if="isHideColumn('ctime')" align="left" show-overflow-tooltip/>

                    <el-table-column label="操作" prop="btn" v-if="isHideColumn('btn')" width="200px" align="left" fixed="right">
                        <template slot-scope="scope">
                            <el-button @click="handleAddOrUpdateVisible(scope.row.id)" type="text" size="small">编辑</el-button>
                            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>

                </el-table>

                <!-- 分页 -->
                <TablePagination
                    :total.sync="total"
                    :page.sync="searchForm.page"
                    :limit.sync="searchForm.limit"
                    @handleSizeChange="handleSizeChange"
                    @handleCurrentChange="handleCurrentChange"/>
            </el-card>

            <!-- 弹窗, 新增 / 修改 -->
            <AddOrUpdate ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>

        </div>

        <!-- 批量设置库存 -->
        <el-dialog
            width="30%"
            title="批量设置库存"
            :close-on-click-modal="false"
            :visible.sync="batckStock.dialogVisible">

            <el-input placeholder="请输入内容" clearable v-model="batckStock.stock"></el-input>

            <div slot="footer" class="dialog-footer">
                <el-button @click="batckStock.dialogVisible = false">关 闭</el-button>
                <el-button @click="handleBatchStock" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>
        </el-dialog>



    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TablePagination from '@/views/components/table/table-pagination'
import AddOrUpdate from './add-or-update-dialog'
export default {
    mixins: [TableMixin],
    components: {
        TablePagination,
        AddOrUpdate
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loading: false,
            submitLoading: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null,
                agentId: null,
                skuId: null,
                skuName: null,
                skuAttr: null,
                stock: null,
                stockLocked: null,
                ctime: null,
                utime: null
            },
            detail: {
                id: null,
                agentId: null,
                skuId: null,
                skuName: null,
                skuAttr: null,
                stock: null,
                stockLocked: null,
                ctime: null,
                utime: null
            },

            agentListSearch: [],

            // 商家拒绝收货
            batckStock: {
                dialogVisible: false,
                ids: null,
                stock: null,
            },
        }
    },

    created() {
        this.getAgentist()
    },

    mounted() {
        this.$nextTick(() => {
            this.getDataList()
        })
    },

    methods: {



        /**
         * 新增或编辑
         */
        handleAddOrUpdateVisible(id) {
            // 表单窗口
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/mall/wmswaresku/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 批量设置库存
         */
        async handleBatchStockDialog() {
            this.batckStock.dialogVisible = true

            let ids = []
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要设置的SKU！', type: 'info' });
            } else {
                this.batckStock.ids = ids
            }
        },

        /**
         * 提交库存设置
         */
        async handleBatchStock() {
            const confirm = await this.$confirm('确定要提交？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/wmswaresku/batchStock',
                    method: 'POST',
                    data: {
                        ids: this.batckStock.ids,
                        stock: this.batckStock.stock
                    }
                })
                if (this.ResultMessage(res)) {
                    this.batckStock.dialogVisible = false
                    this.batckStock.ids = null
                    this.batckStock.stock = null
                    this.getDataList()
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList() {
            this.loading = true
            const res = await this.$http({
                url: '/mall/wmswaresku/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records || []
                this.total = res.data.total
            }
            this.loading = false
        },

        /**
         * 获取代理商
         */
        async getAgentist() {
            this.loading = true
            const res = await this.$http({
                url: '/mall/wmsagentinfo/list',
                method: 'GET'
            })
            if (this.ResultMessage(res, false)) {
                this.agentListSearch = res.data || []
            }
            this.loading = false
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            this.$refs.searchForm1.resetFields()
            this.getDataList()
        }

    }

}
</script>

<style lang="scss" scoped>
</style>
